<?php

namespace App\Models\User\Repository;

use App\Models\WeXin\Model\WxOperation;
use Illuminate\Database\Eloquent\Model;

class WxOperationRepository extends Model{

    protected $use_appid;
    protected $use_sercet;
    protected $init_flag;

    public function __construct($company_token="")
    {
        parent::__construct();
        $this->setCompany($company_token);
    }

    //设置初始化
    public function setCompany($company_token)
    {
        $wx_operation = WxOperation::where('company_token',$company_token)->get();
        if(isEmpty($wx_operation)){
            $this->use_appid = "";
            $this->use_sercet= "";
            $this->init_flag = false;
        }else{
            $wx_operation = $wx_operation->first();
            $this->use_appid = $wx_operation->appid;
            $this->use_sercet= $wx_operation->sercet;
            $this->init_flag = false;
        }
    }

    public function getOpenId($code)
    {
        $appid = $this->use_appid;
        $secret = $this->use_sercet;
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";
        $content=file_get_contents($url);
        $content = json_decode($content, true);
        if($content['session_key']){

        }else{

        }
    }
}

